﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 权限服务
    /// </summary>
    public class FunctionService : IFunctionService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Function model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Updateable<T_Function>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_Function GetById(string id)
        {
            return dal.Get<T_Function>(id);
        }

        public List<T_Function> GetList(string companyId, string parentId)
        {
            Expression<Func<T_Function, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyId))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyId, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(parentId))
            {
                whereExp = whereExp.Compose((a) => a.ParentID == parentId, Expression.AndAlso);
            }
            else
            {
                whereExp = whereExp.Compose((a) => a.ParentID == null, Expression.AndAlso);
            }
            return dal.Queryable<T_Function>().Where(whereExp).OrderBy(a => a.FunctionIndex, OrderByType.Asc).ToList();
        }

        public List<T_Function> GetList(string roleId)
        {
            Expression<Func<T_Role_Function, T_Function, bool>> whereExp = (a, b) => b.DataStatus == DataStatusEnum.Valid && a.RoleID == roleId;
            return dal.Queryable<T_Role_Function, T_Function>((a, b) => new object[]
            {
                JoinType.Left,a.FunctionID == b.ID,
            })
             .Where(whereExp)
            .Select((a, b) => b)
            .ToList();
        }

        public PageData<T_Function> Query(string functionKey, string functionName, string parentId, string companyId, PageInfo page)
        {
            int totalCount = 0;
            Expression<Func<T_Function, T_Function, bool>> whereExp = (a, b) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(functionKey))
            {
                whereExp = whereExp.Compose((a, b) => a.FunctionKey.StartsWith(functionKey), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(functionName))
            {
                whereExp = whereExp.Compose((a, b) => a.FunctionName.StartsWith(functionName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(parentId))
            {
                whereExp = whereExp.Compose((a, b) => a.ParentID == parentId, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(companyId))
            {
                whereExp = whereExp.Compose((a, b) => a.CompanyID == companyId, Expression.AndAlso);
            }

            var list = dal.Queryable<T_Function, T_Function>((a, b) => new object[]
            {
                JoinType.Left,a.ParentID == b.ID,
            })
            .Where(whereExp)
            .OrderBy(a => a.FunctionIndex, OrderByType.Asc)
            .Select((a, b) => new T_Function()
            {
                ID = a.ID,
                FunctionKey = a.FunctionKey,
                FunctionName = a.FunctionName,
                FunctionUrl = a.FunctionUrl,
                FunctionIndex = a.FunctionIndex,
                FunctionIcon = a.FunctionIcon,
                DataStatus = a.DataStatus,
                CreateTime = a.CreateTime,
                ParentName = b.FunctionName,
            })
            .ToPageList(page.PageIndex, page.PageSize, ref totalCount);
            page.DataCount = totalCount;
            return new PageData<T_Function>() { Data = list, Page = page };
        }

        public bool Update(T_Function model)
        {
            return dal.Updateable<T_Function>(p => new T_Function()
            {
                FunctionKey = model.FunctionKey,
                FunctionName = model.FunctionName,
                FunctionUrl = model.FunctionUrl,
                FunctionIndex = model.FunctionIndex,
                FunctionIcon = model.FunctionIcon,
                FunctionDescription = model.FunctionDescription,
                ParentID = model.ParentID,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName,
            }).Where(p => p.ID == model.ID).ExecuteCommand() > 0;
        }
    }
}
